package pl.gwt.server;
//
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbConn {
	
	private int serverLocation = 0;
	
	public DbConn() {	
		//getServerMysqlOn();
	}

	private void getServerMysqlOn() {

		String hostname = null;

		try {
			// Execute command
			String command = "hostname";
			Process child = Runtime.getRuntime().exec(command);

			// Get the input stream and read from it
			java.io.InputStream in = child.getInputStream();

			hostname = "";
			int c;
			while ((c = in.read()) != -1) {
				hostname += (char) c;
			}
			in.close();
			child.destroy();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (hostname.startsWith("de")) { 
			
			this.serverLocation = 1; // ex.: desktop
		
		} else if (hostname.startsWith("la")) {
			
			this.serverLocation = 2; // ex.: laptop
			
		} else {			
			this.serverLocation = 0; // production server			
		}
	}
	

	public Connection getConn() {	
		    Connection conn	= null;	    
		    String url 		= "jdbc:jtds:sqlserver://127.0.0.1:1434/";//getServerURL();
		    
		    String db 		= "YOGWTCRMDB";
		    String driver 	= "net.sourceforge.jtds.jdbc.Driver";
		    String user 	= "wozhd";
		    String pass 	= "mass_adm1n";	    
		    
		    try {
		      
		    	Class.forName(driver).newInstance();
		    	// jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
		    	conn = DriverManager.getConnection(url+db, user, pass);		      
		    } catch (Exception e) {		    	
		    	System.err.println("MS Server Connection Error: ");	    	
		    	e.printStackTrace();
		    }
		
		    return conn;
	}
	
	private String getServerURL() {
		String url = null;
		if (serverLocation == 1) { // apacz			
			url = "";			
		} else if (serverLocation == 2) { // msi			
			url = "";			
		} else { //desktop			
			url = "jdbc:jtds:sqlserver://127.0.0.1:1434/";			
		}
		
		return url;
	}

	protected static int getResultSetSize(ResultSet resultSet) {
	    int size = -1;

	    try {
	        resultSet.last();
	        size = resultSet.getRow();
	        resultSet.beforeFirst();
	    } catch(SQLException e) {
	        return size;
	    }

	    return size;
	}
		
	public String testConnection(){
	
		String query = "SELECT * FROM clients";
		String s = ""; 
		
		try {
		    Connection conn = this.getConn();
		    Statement select = conn.createStatement();
		    ResultSet result = select.executeQuery(query);
		    while (result.next()) {
		        s += result.getString(1);
		    }
		    select.close();
		    result.close();
		    conn.close();
		} catch(SQLException e) {
		        System.err.println("MS SQL Statement Error: " + query);
		        e.printStackTrace();
		}
		return s;	
	}

}